/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.swing.JButton;
import model.Base;
import model.Book;
import model.CD;
import model.DVD;
import model.Document;
import model.Magazine;
import model.NewsPaper;
import view.DocumentPropertiesView;

/**
 *
 * @author Ira
 */
public class DocumentPropertiesController implements ActionListener {

	private Base model;
	private DocumentPropertiesView view;
	
	public DocumentPropertiesController(Base model, DocumentPropertiesView view) {
		this.model = model;
		this.view = view;
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		String name = ((JButton)e.getSource()).getName();
		if(name.equals(DocumentPropertiesView.BTN_BORROW)) {
			this.model.borrow(this.view.getDoc());
			((JButton)e.getSource()).setVisible(false);
		}
		else if(name.equals(DocumentPropertiesView.BTN_RESERVE)) {
			this.model.reserve(this.view.getDoc());
			((JButton)e.getSource()).setVisible(false);
		}
		else if(name.equals(DocumentPropertiesView.BTN_SAVE)) {
			if(!this.model.isDocumentModifiable(this.view.getDoc())) {
				return;
			}
			String type = this.view.getTxtCategory();
			Document newDoc = null;
			List<String> authors = new ArrayList<String>(Arrays.asList(this.view.getTxtAuthors().split(", ")));
			Short year = null;
			try {
				year = Short.parseShort(this.view.getTxtYear());
			}
			catch(Exception ex) {
				return;
			}
			
			if(type.equals("Livre")) {
				int nbPage;
				try {
					nbPage = Integer.parseInt(this.view.getBook().getTxtNbPage());
				}
				catch(Exception ex) {
					return;
				}
				newDoc = new Book(this.view.getTxtTitle(), year, this.view.getBook().getTxtType(), authors, this.view.getTxtEditor(), nbPage);
			}
			else if(type.equals("CD")) {
				int lenght;
				try {
					lenght = Integer.parseInt(this.view.getCd().getTxtLenght());
				}
				catch(Exception ex) {
					return;
				}
				newDoc = new CD(this.view.getTxtTitle(), year, this.view.getCd().getTxtType(), authors, this.view.getTxtEditor(), lenght);
			}
			else if(type.equals("DVD")) {
				int lenght;
				try {
					lenght = Integer.parseInt(this.view.getDvd().getTxtLenght());
				}
				catch(Exception ex) {
					return;
				}
				newDoc = new DVD(this.view.getTxtTitle(), year, this.view.getDvd().getTxtType(), authors, this.view.getTxtEditor(), lenght);
			}
			else if(type.equals("journal")) {
				newDoc = new NewsPaper(this.view.getTxtTitle(), year, authors, this.view.getTxtEditor(), this.view.getNewsPaper().getTxtDayDate());
			}
			else if(type.equals("Magazine")) {
				int nbPage;
				try {
					nbPage = Integer.parseInt(this.view.getBook().getTxtNbPage());
				}
				catch(Exception ex) {
					return;
				}
				newDoc = new Magazine(this.view.getTxtTitle(), year, authors, this.view.getTxtEditor(), nbPage);
			}
			this.model.DocumentMaj(this.view.getDoc(), newDoc);
		}
		else if(name.equals(DocumentPropertiesView.BTN_DELETE)) {
			if(this.model.isDocumentModifiable(this.view.getDoc())) {
				this.model.DocumentDelete(this.view.getDoc());
				((JButton)e.getSource()).setVisible(false);
			}
		}
		else if(name.equals(DocumentPropertiesView.BTN_BRING_BACK)) {
			this.model.bringBack(this.view.getDoc());
			((JButton)e.getSource()).setVisible(false);
		}
		else if(name.equals(DocumentPropertiesView.BTN_NO_RESERVE)) {
			this.model.noReserve(this.view.getDoc());
			((JButton)e.getSource()).setVisible(false);
		}
	}
	
}
